import ij.ImagePlus;
import ij.process.ImageProcessor;

import java.awt.*;
import java.util.*;

public class Region {
    LinkedList<Block> blocks;
    ImagePlus textureImage;
    LinkedList<CoocurenceMatrix> matrices;
    int color = 0;
    int id = 0;

    public Region(int i) {
        id = i;
        blocks = new LinkedList<Block>();
        matrices = new LinkedList<CoocurenceMatrix>();
    }

    public Region(ImagePlus ip, int i, LinkedList<Block> b){
        blocks = b;
        textureImage = ip;
        id = i;
        matrices = new LinkedList<CoocurenceMatrix>();
    }

    public void show(){
        if( textureImage != null)
            textureImage.show();
    }

    public void colorRegion(ImageProcessor imageProcessor){
        Random rand = new Random();

        if(color == 0){
            color = (rand.nextInt(256)<<16)|(rand.nextInt(256)<<8)|rand.nextInt(256);
        }

        for(Block bl : blocks){
            bl.colorEdges(imageProcessor, id, color);
        }
    }

    public void generateMatrices(){
        if(blocks.size()==0){
            return;
        }

        Block b = blocks.get(0);

        for(CoocurenceMatrix c : b.matrices){
            matrices.push(new CoocurenceMatrix(c.levels));
        }

        int i;
        for(Block bl : blocks){
            i = 0;
            for(CoocurenceMatrix c : bl.matrices){
                matrices.get(i).add(c);
                ++i;
            }
        }
    }

    public HashMap<String, Double> getMaxValues(){
        HashMap<String, Double> ret = new HashMap<String, Double>();
        double maxKorelacja = 0, maxMoment = 0, maxKontrast = 0, maxEntropia = 0;
        for(CoocurenceMatrix c : matrices){
            maxKorelacja = maxKorelacja<c.korelacja?c.korelacja:maxKorelacja;
            maxEntropia = maxEntropia<c.entropia?c.entropia:maxEntropia;
            maxKontrast = maxKontrast<c.kontrast?c.kontrast:maxKontrast;
            maxMoment = maxMoment<c.moment?c.moment:maxMoment;
        }

        ret.put("korelacja",maxKorelacja);
        ret.put("kontrast",maxKontrast);
        ret.put("entropia",maxEntropia);
        ret.put("moment",maxMoment);

        return ret;
    }
}